package com.xiaohu.controller;

import com.xiaohu.common.ResultEntity;
import com.xiaohu.entity.Dept;
import com.xiaohu.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("dept")
public class DeptController {

    @Autowired//spring自动为其创建对象，不用再new
    private DeptService deptService;

    /**
     * 跳转部门列表页面
     *
     * @return 列表页面的路径
     */
    @RequestMapping("/goList")
    public String goDept() {
        return "dept/list";
    }

    /**
     * 查询所有部门数据
     * Request URL: http://localhost:8089/dept/selectAll?page=1&limit=15&title=&address=
     *
     * @param dept 查询条件
     * @return 部门数据
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public ResultEntity selectAll(Dept dept) {
        ResultEntity resultEntity = deptService.selectAll(dept);
        return resultEntity;
    }

    /**
     * 将部门数据，展示成树形结构
     *
     * @return
     */
    @RequestMapping("/tree")
    @ResponseBody
    public ResultEntity tree() {
        ResultEntity resultEntity = deptService.tree();
        return resultEntity;
    }

    /**
     * 部门下拉框查询
     *
     * @return
     */
    @RequestMapping("/deptSelect")
    @ResponseBody
    public ResultEntity deptSelect() {
        ResultEntity resultEntity = deptService.deptSelect();
        return resultEntity;
    }

    /**
     * 跳转新增页面
     *
     * @return
     */
    @RequestMapping("/goAdd")
    public String goAdd() {
        return "dept/add";
    }

    /**
     * 保存部门信息
     *
     * @param dept todo 页面传来的JSON数据｛｝会自动转换成Dept数据
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity add(@RequestBody Dept dept) {
        ResultEntity resultEntity = deptService.add(dept);

        return resultEntity;
    }

    /**
     * 跳转编辑页面
     *
     * @return 编辑页面的路径
     */
    @GetMapping("/goEdit")
    public String goEdit() {
        return "dept/edit";
    }

    /**
     * 编辑部门数据
     *
     * @return 统一级结果返回值
     */
    @PostMapping(value = "/edit")
    @ResponseBody
    public ResultEntity edit(@RequestBody Dept dept) {
        ResultEntity resultEntity = deptService.edit(dept);
        return resultEntity;
    }

    /**
     * 删除部门
     *
     * @return 统一返回值
     */
    @GetMapping(value = "/delete")
    @ResponseBody
    public ResultEntity delete(String id) {
        return deptService.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids 部门id结婚
     * @return 统一返回值
     */
    @PostMapping(value = "/batchDelete")
    @ResponseBody
    public ResultEntity batchDelete(@RequestBody List<String> ids) {
        return deptService.batchDelete(ids);
    }

}
